package cn.e2win.biz.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysCompany;
import cn.e2win.base.entity.SysDict;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.security.UserSession;
import cn.e2win.biz.sys.Dict;
import cn.e2win.biz.util.StringUtils;


@Controller
@Scope("request")
@RequestMapping("company")
public class CompanyController  extends BaseController {

	@Autowired
	private CompanyService companyService;


	@Override
	public BaseService initService() {
		
		return companyService;
	}

	@RequestMapping("companyPager")
	public String companyPager(HttpServletRequest request, @PageParam Pager pager,
			ModelMap modelMap) {
		try {
			//pager.setNumPerPage(3);
			companyService.pagerCompany(pager);
			modelMap.put("pager", pager);
			return "company/companyPager";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}

	// 加载添加/修改销售订单页面
	@RequestMapping("toEditBaseInfo")
	public String toEditBaseInfo(HttpServletRequest request,  ModelMap modelMap) {
		try {
			SysCompany obj = (SysCompany)companyService.getEntity(SysCompany.class,UserSession.getUser().getCoId());
			modelMap.put(OBJECT, obj);

			return "company/companyInfo";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}


	// 加载添加/修改销售订单页面
	@RequestMapping("editBaseInfo")
	@ResponseBody
	public Result editBaseInfo(HttpServletRequest request,  ModelMap modelMap) {
		Result r = new Result();
		try {
			SysCompany obj = (SysCompany)companyService.getEntity(SysCompany.class,UserSession.getUser().getCoId());
			obj.setOrderFoot(request.getParameter("orderFoot"));
			Integer printCustInfoInt = Integer.parseInt(request.getParameter("printCustInfoInt"));
			obj.setPrintCustInfoInt(printCustInfoInt);
			
			
			obj = (SysCompany)companyService.editBaseInfo(obj);
			modelMap.put(OBJECT, obj);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			r.setStatusCode(Result.FAILED);
		}
		return r;
	}


	// 加载添加/修改销售订单页面
	@RequestMapping("toEdit/{id}")
	public String toEdit(HttpServletRequest request, 
			@PathVariable(ID) Integer id, ModelMap modelMap) {
		try {

			SysCompany obj = (SysCompany)companyService.getEntity(SysCompany.class,id);
			modelMap.put(OBJECT, obj);

			return "company/companyEdit";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}


	// 加载添加/修改销售订单页面
	@RequestMapping("edit")
	@ResponseBody
	public Result edit(HttpServletRequest request, 
			@ModelAttribute SysCompany obj, ModelMap modelMap) {
		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_" + modelMap.get(FUNID));
		try {
			
			obj = companyService.editCompany(obj);			
			modelMap.put(OBJECT, obj);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			r.setStatusCode(Result.FAILED);
		}
		return r;
	}
	
	
	
	

	//基础数据树
	@RequestMapping("dictTree")
	public String dictTree(HttpServletRequest request, @PageParam Pager pager,
			ModelMap modelMap) {
		try {
			List list = companyService.dictTree();
			modelMap.put("dictTree", list);
			return "company/dictTree";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}


	//基础数据列表
	@RequestMapping("dictPager/{type}")
	public String dictPager(HttpServletRequest request,@PathVariable("type") String type,
			@PageParam Pager pager, ModelMap modelMap) {
		if(!pager.getScMap().containsKey("activeStatusInt")){
			pager.getScMap().put("activeStatusInt", "1");
		}
		String returnJsp = "prod/productInfoPager";
		try {
			if("dictList".equals(type)){
				modelMap.put("prodPath", null);//((Map<Object,List> )Dict.get("PRODUCT_PATH")).get(pager.getScMap().get("level_I")+"_" + pager.getScMap().get("prodId")));
				returnJsp = "company/dictList";
				//pager.setReturnAll(true);
			}
			pager.setAction(request.getRequestURL() +"?" + request.getQueryString());
			companyService.pagerDict(pager);
			modelMap.put("pager", pager);
			modelMap.put("type", type);
			return returnJsp;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}
	// 加载添加/修改产品页面
	@RequestMapping("toEditDict/{id}")
	public String toEditDict(HttpServletRequest request,
			@PathVariable(ID) Integer id, ModelMap modelMap) {
		try {
			SysDict obj = (SysDict)companyService.getEntity(SysDict.class,id);
			if(obj==null){
				String myType = request.getParameter("myType");
				if(StringUtils.isEmpty(myType)){
					throw new MyException("请先选择数据类型!");
				}
				obj = new SysDict();
				obj.setMyType(myType);
			}
			modelMap.put(OBJECT, obj);

			return "company/dictEdit";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}

	// 添加/修改产品
	@RequestMapping("editDict")
	@ResponseBody
	public Result editDict(HttpServletRequest request,
			@ModelAttribute SysDict obj, ModelMap modelMap) {
		return updateDict(request,obj,modelMap);
	}

	// 删除
	@RequestMapping("delDict/{id}")
	@ResponseBody
	public Result delDict(HttpServletRequest request,
			@PathVariable(ID) Integer id, ModelMap modelMap) {
		SysDict obj = (SysDict)companyService.getEntity(SysDict.class, id);
		obj.setActiveStatusInt(0);
		return updateDict(request,obj,modelMap);
	}

	
	public Result updateDict(HttpServletRequest request,
			 SysDict obj, ModelMap modelMap) {

		Result r = new Result();
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_" + modelMap.get(FUNID));
		try {
			companyService.editDict(obj);
			Dict.refresh(obj.getMyType());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			r.setStatusCode(Result.FAILED);
			//return r;
		}
		return r;
	}
	

}
